<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>14题：计算总和</title>
    <script>
        function caculate(flag) {
            var num = prompt(flag ? '请输入正确的三位数' : '请输入一个三位数');
            if (!/^[0-9]*$/i.test(num)) {
                caculate(true);
                return;
            }
            let total = 0;
            for (let i = 0; i < num.length; i++) {
                let n = parseInt(num[i]);
                total += n;
            }
            alert(`各个位数的总和为${total}`);
        }
    </script>
</head>
<body>
    <p>
        请输入一个三位数<input type="button" value="点击输入" onclick="caculate()">
    </p>
</body>
</html>